﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class NewGoodsDetails : Form
    {
        Util.ActionType _actType = DIProject.Util.ActionType.NoAction;
        Int32? _goodsId = null;
        Int32 or_id = 0;
        DateTime or_md = new DateTime(1900, 1, 1);
        public NewGoodsDetails(Util.ActionType actType, Int32? goodsId)
        {
            _actType = actType;
            _goodsId = goodsId;
            InitializeComponent();
        }

        private void selecter1_MySelectButtonClick(object sender, EventArgs e)
        {
            NewAssortment na = new NewAssortment();
            DialogResult dr = na.ShowDialog();
            if (dr == DialogResult.OK)
            {
                selecter1.SelectedValue = na.SelectedId;
                selecter1.SelectedText = na.SelectedName;
            }
        }
        private void InitComboBoxes()
        {
            cbCountry.DataSource = DIProject.Data.Country.Data;
            cbManufacturer.DataSource = DIProject.Data.Manufacturer.Data;
        }

        private void NewGoodsDetails_Load(object sender, EventArgs e)
        {
            InitComboBoxes();
            panel1.Visible = Data.Sync.IsAdmin;
            switch (_actType)
            {
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                default :
                    break;
            }
        }

        private void btnInsertUpdate_Click(object sender, EventArgs e)
        {
            if (!this.MyFormValidate())
            {
                return;
            }
            switch (_actType)
            {
                case DIProject.Util.ActionType.Update:
                    GoodModifi();
                    break;
                case DIProject.Util.ActionType.Insert:
                    GoodAdd();
                    break;
                default:
                    break;
            }
        }
        private void InitUpdate()
        {
            btnInsertUpdate.Text = "Изменить";
            try
            {
                goodTableAdapter.Fill(this.newDist1.Good, _goodsId.Value);
                DataSets.NewDist.GoodRow gr = (this.bs.Current as DataRowView).Row as DataSets.NewDist.GoodRow;
                or_id = gr.Id;
                or_md = gr.MD;
            }
            catch (Exception ee)
            {
                System.Windows.Forms.MessageBox.Show(ee.Message);
            }
        }
        private bool MyFormValidate()
        {
            if (selecter1.SelectedValue == null)
            {
                MessageBox.Show("Необходимо выбрать препарат");
                return false;
            }
            string str = txbFixedPrice.Text;
            if (!string.IsNullOrEmpty(str))
            {
                decimal d;
                if (!decimal.TryParse(str, out d))
                {
                    txbFixedPrice.SelectAll();
                    MessageBox.Show("Необходимо ввести вещественное число");
                    return false;
                }
            }
            return true;
        }
        private void InitInsert()
        {
            btnInsertUpdate.Text = "Сохранить";
            DataSets.NewDist.GoodRow gr = this.newDist1.Good.NewGoodRow();
            this.newDist1.Good.Rows.Add(gr);
        }
        private void GoodModifi()
        {
            decimal? fp = null;
            string str = txbFixedPrice.Text;
            if(!string.IsNullOrEmpty(str))
            {
                decimal d;
                decimal.TryParse(str,out d);
                fp = d;
            }
            DataSets.NewDist.GoodRow gr = (this.bs.Current as DataRowView).Row as DataSets.NewDist.GoodRow;
            Int32 res = Data.Goods.GoodsModifi((Int32)selecter1.SelectedValue, (Int32)cbManufacturer.SelectedValue,
                (Int32)cbCountry.SelectedValue, Convert.ToInt32(txbWholesalePack.Text), Convert.ToInt32(txbRetailPack.Text),
                null, cbxAllowUnPack.Checked, txbManufactureBarCode.Text, fp, cbxDelete.Checked, cbAgregate.Checked, txbNote.Text,
                DateTime.Now, FirstForm.Login, or_id, or_md);
        }
        private void GoodAdd()
        {
            DataSets.NewDist.GoodRow gr = (this.bs.Current as DataRowView).Row as DataSets.NewDist.GoodRow;
            decimal? fp = null;
            string str = txbFixedPrice.Text;
            if (!string.IsNullOrEmpty(str))
            {
                decimal d;
                decimal.TryParse(str, out d);
                fp = d;
            }
            Int32 res = Data.Goods.NewGood((Int32)selecter1.SelectedValue, (Int32)cbManufacturer.SelectedValue,
                (Int32)cbCountry.SelectedValue, Convert.ToInt32(txbWholesalePack.Text), Convert.ToInt32(txbRetailPack.Text),
                null, cbxAllowUnPack.Checked, txbManufactureBarCode.Text, fp, cbAgregate.Checked, txbNote.Text,
                DateTime.Now, FirstForm.Login);
        }

        private void txbWholesalePack_Validating(object sender, CancelEventArgs e)
        {
            string str = txbWholesalePack.Text;
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("Введите целое число");
                txbWholesalePack.SelectAll();
                e.Cancel = true;
                return;
            }
            Int32 d;
            if(!Int32.TryParse(str,out d))
            {
                MessageBox.Show("Введите целое число");
                txbWholesalePack.SelectAll();
                e.Cancel = true;
                return;
            }
        }

        private void txbRetailPack_Validating(object sender, CancelEventArgs e)
        {
            string str = txbRetailPack.Text;
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("Введите целое число");
                txbWholesalePack.SelectAll();
                e.Cancel = true;
                return;
            }
            Int32 d;
            if (!Int32.TryParse(str, out d))
            {
                MessageBox.Show("Введите целое число");
                txbWholesalePack.SelectAll();
                e.Cancel = true;
                return;
            }
        }
    }
}